import 'package:hrf_app/common/index.dart';
import 'package:hrf_app/common/services/user.dart';
import 'package:hrf_app/pages/main_tab/main_menu_tab/controller.dart';
import 'package:flutter/material.dart';

class MenuDataHandler {
  List<MenuItemModel> initializeMenuList() {
    List<MenuItemModel> menuList = [];
    getMaterialMenuList(menuList);
    getToolMenuList(menuList);
    // getProjectMenuList(menuList);
    List? roleList = UserService.to.profile.perms;
    if (roleList == null) {
      return [];
    }
    List<MenuItemModel> menuListResult = digui(menuList, roleList);
    return menuListResult;
  }

//递归判断是否展示菜单根据字符串权限
  List<MenuItemModel> digui(List<MenuItemModel> list, List<dynamic> roles) {
    List<MenuItemModel> topList = [];
    for (MenuItemModel vo in list) {
      if (roles.contains(vo.hasPerm)) {
        topList.add(vo);
        if (vo.children != null && vo.children!.isNotEmpty) {
          List<MenuItemModel> childrenList = digui(vo.children!, roles);
          vo.children = childrenList;
        }
      }
    }
    return topList;
  }

  //bug-路由
  getMaterialMenuList(List<MenuItemModel> menuList) {
    MenuItemModel material = MenuItemModel(
        name: "配件管理",
        color: Colors.black,
        darkColor: Colors.black,
        router: "",
        hasPerm: "sys:menu:material");
    List<MenuItemModel> bugChildrenList = [];
    MenuItemModel bugItem = MenuItemModel(
        name: "配件查询",
        color: Colors.blue,
        darkColor: Colors.black,
        router: RouteNames.materialSearchListPage,
        params: {"status": "0"},
        hasPerm: "sys:menu:material:search");
    bugChildrenList.add(bugItem);
    MenuItemModel bugItem2 = MenuItemModel(
        name: "配件新增",
        color: Colors.lightGreen,
        darkColor: Colors.black,
        router: RouteNames.materialAddPage,
        params: {"status": "10"},
        hasPerm: "sys:menu:material:add");
    bugChildrenList.add(bugItem2);
    material.children = bugChildrenList;
    menuList.add(material);
  }
  //快捷工具
  getToolMenuList(List<MenuItemModel> menuList) {
    MenuItemModel bug = MenuItemModel(
        name: "快捷工具",
        color: Colors.black,
        darkColor: Colors.black,
        router: "",
        hasPerm: "sys:menu:tool");

    List<MenuItemModel> bugChildrenList = [];
    MenuItemModel bugItem = MenuItemModel(
        name: "电话水印",
        color: Colors.blue,
        darkColor: Colors.black,
        router: RouteNames.imageWater,
        params: {"status": "0"},
        hasPerm: "sys:menu:tool:addWater");
    bugChildrenList.add(bugItem);
    MenuItemModel bugCompanyItem = MenuItemModel(
        name: "公司水印",
        color: Colors.blue,
        darkColor: Colors.black,
        router: RouteNames.imageCompanyWater,
        params: {"status": "0"},
        hasPerm: "sys:menu:tool:addWater");
    bugChildrenList.add(bugCompanyItem);
    bug.children = bugChildrenList;
    menuList.add(bug);
  }
  //实验室
  // getProjectMenuList(List<MenuItemModel> menuList) {
  //   MenuItemModel bug = MenuItemModel(
  //       name: "项目管理",
  //       color: Colors.black,
  //       darkColor: Colors.black,
  //       router: "",
  //       hasPerm: "sys:menu:project");
  //
  //   List<MenuItemModel> bugChildrenList = [];
  //   MenuItemModel bugItem = MenuItemModel(
  //       name: "项目管理",
  //       color: Colors.blue,
  //       darkColor: Colors.black,
  //       router: RouteNames.projectList,
  //       params: {"status": "0"},
  //       hasPerm: "sys:menu:project:list");
  //   bugChildrenList.add(bugItem);
  //   bug.children = bugChildrenList;
  //   menuList.add(bug);
  // }
}
